package com.api.server.controller;


import com.api.server.pojo.Field;
import com.api.server.pojo.RespBean;
import com.api.server.pojo.Result;
import com.api.server.service.IFieldService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shi
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/field")
public class FieldController {

    @Autowired
    private IFieldService fieldService;


    //根据场地id查询场地信息
    @ApiOperation(value = "根据场地id查询场地信息")
    @PostMapping("/message")
    public RespBean message(String fieldnumber){
        Field field = fieldService.message(fieldnumber);
        return Result.success("获取场地信息成功！",field);
    }


    //增加场地
    @ApiOperation(value = "增加场地")
    @PostMapping
    public Result add(@RequestBody Field field){
        if (fieldService.message(field.getFieldnumber())!=null){
            return Result.fail("该场地编号已经被使用！");
        }
        fieldService.add(field);
        return Result.success(201,"场地创建成功",field);
    }


    //根据id删除场地
    @ApiOperation(value = "删除场地")
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") String id){
        Field field = fieldService.getById(id);
        if (field.getFieldnumber()==null){
            return Result.fail("不存在该场地！");
        }
        fieldService.delete(id);
        return RespBean.success("删除场地成功！");
    }


    //根据场馆查询场地
    @ApiOperation(value = "根据场馆查询场地")
    @GetMapping("/{id}")
    public RespBean equByCdId(@PathVariable("id") String id){
        List<Field> fields = fieldService.equByCdId(id);
        return RespBean.success("查询成功！",fields);
    }


    //根据id编辑场地信息
    @ApiOperation(value = "根据id编辑场地信息")
    @PutMapping("/updata")
    public RespBean updataById(@RequestBody Field field){
        int row = fieldService.updataById(field);
        return RespBean.success("修改成功！",row);
    }
}
